package com.example.obs.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.obs.model.domin.User;
import com.example.obs.service.UserService.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;

@Api(tags = "用户个人信息接口")
@RequestMapping("/person")
@RestController
@PreAuthorize("isAuthenticated()")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @param
     * @return
     */
    @PreAuthorize("permitAll")
    @RequestMapping(value="/login",method= RequestMethod.POST)
    public User login(@RequestBody User user) {
        User user1 = userService.login(user);
        return user1;
    }
    @PreAuthorize("permitAll")
    @ResponseBody
    @PostMapping("/LoginVf")
    public String LoginVf(String ticket, String randstr, HttpServletRequest req, HttpSession httpSession) {
        String aid = "2050027191";
        String AppSecretKey = "0O1eQXPho13aAoQeAYWxZ1A**";
        String vfurl = "https://ssl.captcha.qq.com/ticket/verify";
        String userIp = req.getRemoteAddr();
        if (userIp.equals("127.0.0.1") || userIp.equals("0:0:0:0:0:0:0:1")) {
            //根据网卡取本机配置的IP
            InetAddress inet = null;
            try {
                inet = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            userIp = inet.getHostAddress();
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet;
        CloseableHttpResponse response = null;
        try {
            httpGet = new HttpGet(vfurl + "?aid=" + aid + "&AppSecretKey=" + AppSecretKey + "&Ticket=" + URLEncoder.encode(ticket, "UTF-8") + "&Randstr=" + URLEncoder.encode(randstr, "UTF-8") + "&UserIP=" + URLEncoder.encode(userIp, "UTF-8"));
            response = httpclient.execute(httpGet);

            HttpEntity entity = response.getEntity();
            System.out.println(entity);
            if (entity != null) {
                String res = EntityUtils.toString(entity);
                System.out.println(res); // 临时输出
                JSONObject result = JSON.parseObject(res);
                // 返回码
                int code = result.getInteger("response");
                // 恶意等级
                int evilLevel = result.getInteger("evil_level");

                if (evilLevel > 60) {
                    System.out.println("验证操作危险数较高,IP:" + userIp);
                }

                // 验证成功
                if (code == 1) {
                    httpSession.setAttribute("loginVfCode", 1);
                    return "1";
                }
            }
        } catch (java.io.IOException e) {
            // 忽略
        } finally {
            try {
                response.close();
            } catch (Exception ignore) {
            }
        }
        return "-1";
    }


    /**
     * @param
     * @return
     */
    @ApiOperation(value = "修改用户密码")
    @RequestMapping(value="/uppsw",method= RequestMethod.POST)
    public int uppsw(@RequestBody User user) {
        userService.uppsw(user);
        return 1;
    }
    @ApiOperation(value = "修改邮箱信息")
    @RequestMapping(value="/upemail",method= RequestMethod.POST)
    public int upemail(@RequestBody User user) {
        userService.upemail(user);
        return 1;
    }
    @RequestMapping(value="/forgetpwd",method= RequestMethod.POST)
    public int forgetpwd(@RequestBody User user) {
        userService.forgetpwd(user);
        return 1;
    }
    @ApiOperation(value = "修该手机信息")
    @RequestMapping(value="/upphone",method= RequestMethod.POST)
    public int upphone(@RequestBody User user) {
        userService.upphone(user);
        return 1;
    }
}
